﻿using BaseUtils.Helper;
using Service.src.Flow.Server;
using Service.src.NW.Model;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Service.src.Flow.Api
{

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class GlobalParamApi
    {

        public static GlobalParamService ParamServer;

        public GlobalParamApi()
        {
            ParamServer = new GlobalParamService();
        }
        /// <summary>
        /// 添加设备，更新设备
        /// </summary>
        /// <param name="GlobalParamJson"></param>
        /// <returns></returns>
        public string AddGlobalParam(string GlobalParamJson)
        {
            GlobalParamEntity GlobalParamInfo = ConvertHelper.ToObject<GlobalParamEntity>(GlobalParamJson);
            if (GlobalParamInfo != null && !string.IsNullOrEmpty(GlobalParamInfo.Id.ToString()) && GlobalParamInfo.Id > 0)
            {
                GlobalParamService.UpldateGlobalParam( GlobalParamInfo);
            }
            else
            {
                GlobalParamInfo = ParamServer.AddGlobalParam( GlobalParamInfo);
            }
            AppData.GlobalParams.Clear();
            new AppData().SetGlobalParams();
            return GlobalParamInfo.ToJson();
        }

        /// <summary>
        /// 更新全局变量
        /// </summary>
        /// <param name="GlobalParamJson"></param>
        /// <returns></returns>
        public bool UpdateGlobalParams(string GlobalParamJson)
        {
            List<GlobalParamEntity> Updates = ConvertHelper.ToObject<List<GlobalParamEntity>>(GlobalParamJson);
            if (Updates == null)
            {
                return false;
            }
            foreach (GlobalParamEntity source in Updates)
            {
                ParamServer.UpdateGlobalParam( source);
            }
            return true;
        }

        public bool UpdateGlobalParam(string GlobalParamJson)
        {
            GlobalParamEntity Update = ConvertHelper.ToObject<GlobalParamEntity>(GlobalParamJson);
            return ParamServer.UpdateGlobalParam(Update);
        }

        /// <summary>
        /// 根据 ID 删除设备
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool DeleteGlobalParam(int Id)
        {
            bool res = ParamServer.DeleteGlobalParam(Id);
            AppData.GlobalParams.Clear();
            new AppData().SetGlobalParams();
            return res;
        }

        /// <summary>
        /// 获取所有设备
        /// </summary>
        /// <returns></returns>
        public string GetGlobalParams()
        {
            return AppData.GlobalParams.ToJson();
        }
    }
}
